package com.example.onion.hxchat.room.repositorywc;

import android.content.Context;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import com.example.onion.hxchat.room.dao.FriendDao;
import com.example.onion.hxchat.room.dao.UserDao;
import com.example.onion.hxchat.room.entity.Friend;
import com.example.onion.hxchat.room.entity.User;
import com.example.onion.hxchat.room.wcdatabase.MyDataBaseWc;
import com.example.onion.hxchat.util.HttpUtils;

import java.io.IOException;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.Request;

/**
 * @author:onion
 * @data: 2020/7/8
 */
public class FriendRepository {
    /**
     * Data list from Dao.
     */
    private LiveData<List<Friend>> friends;

    private MyDataBaseWc database;
    private FriendDao friendDao;

    public FriendRepository(Context context) {
        //获取数据库实例，数据库采用单例模式
        database = MyDataBaseWc.getInstance(context.getApplicationContext());
        friendDao = database.getFriendDao();
        friends = friendDao.getAllFriend();
    }

    /**
     * Gets all data from Master table from database.
     * @return The list of users
     */
    public LiveData<List<Friend>> getAllFriends() {
        return friends;
    }

    /**
     * 删除二者的好友关系
     * @param ownerId 当前登录用户的ownerId
     * @param userId 需要删除与当前用户id有好友关系的userId对象信息
     */
    public void DeleteFriend(String ownerId,String userId)
    {
        new FriendRepository.DeleteFriendAsyncTask(friendDao).execute(ownerId,userId);
    }

    public LiveData<List<Friend>> getUsersFriends(String ownUserId)
    {
        return friendDao.getAllFriendCur(ownUserId);
    }

    public void insertMaster(Friend... friends){
        new FriendRepository.InsertMasterAsyncTask(friendDao).execute(friends);
    }

    //内部类，用于在子线程中访问数据库，如果有多个操作的话，每一个操作都要对应一个AsyncTask
    static class InsertMasterAsyncTask extends AsyncTask<Friend,Void,Void> {

        private FriendDao friendDao;

        public InsertMasterAsyncTask(FriendDao friendDao) {
            this.friendDao = friendDao;
        }

        @Override
        protected Void doInBackground(Friend... friends) {
            friendDao.insertFriend(friends);
            return null;
        }
    }  //内部类，用于在子线程中访问数据库，如果有多个操作的话，每一个操作都要对应一个AsyncTask

    static class DeleteFriendAsyncTask extends AsyncTask<String, Void, Void> {

        private FriendDao friendDao;

        public DeleteFriendAsyncTask(FriendDao friendDao) {
            this.friendDao = friendDao;
        }

        @Override
        protected Void doInBackground(String... strings) {
            friendDao.deleteByOwner(strings[0],strings[1]);
            return null;
        }
    }

    static class GetFriendByNetAsyncTask extends AsyncTask<String, Void, Void> {

        private FriendDao friendDao;

        public GetFriendByNetAsyncTask(FriendDao friendDao) {
            this.friendDao = friendDao;
        }

        @Override
        protected Void doInBackground(String... strings) {
            HttpUtils httpUtils = HttpUtils.getInstance();
            FormBody.Builder builder = new FormBody.Builder();
            builder.add("userId1",strings[0]);
            builder.add("userId2",strings[1]);
            FormBody formBody = builder.build();
            httpUtils.asyncPost("http://49.232.60.155:8080/AppServer_war/GetFriendServlet", formBody, new HttpUtils.HttpCallBack() {
                @Override
                public void onError(Request request, IOException e) {

                }

                @Override
                public void onSuccess(Request request, String result) {

                }
            });
            friendDao.deleteByOwner(strings[0], strings[1]);
            return null;
        }
    }

}
